<template>
  <div class="chat-wrapper">
    <div class="left">
      <div class="back" @click="back">
        <div class="back-icon">
          <Icon type="md-arrow-round-back" color="#487FFF" size="20" />
        </div>
        <p class="back-text">退出聊天</p>
      </div>
      <div class="agent">
        <div class="agent-header">最近使用</div>
        <div class="agent-list">
          <div
            class="agent-item"
            :class="{ active: activeAgentId == item.id }"
            @click="handleAgentChange(item)"
            v-for="item in dataList"
            :key="item.id"
          >
            {{ item.name }}
          </div>
        </div>
      </div>
    </div>
    <div class="right">
      <div class="chat">
        <div class="chat-info">
          <div class="name">{{activeAgentName}}</div>
          <div class="refresh">
            <Button
              type="primary"
              class="add-btn"
              ghost
              shape="circle"
              icon="md-text"
              >新对话</Button
            >
            <Button shape="circle" icon="md-refresh"></Button>
          </div>
          <div class="room-list">
            <div class="room-item">
              <Icon type="md-text" size="16" />
              <div class="room-name">你好</div>
              <div class="time">16:45</div>
            </div>
            <div class="room-item active">
              <Icon type="md-text" size="16" />
              <div class="room-name">你好，我有一</div>
              <div class="time">16:45</div>
            </div>
            <div class="more-text">已加载全部</div>
          </div>
        </div>
        <div class="chat-content">
          <div class="header">
            <div class="chat-name">对话</div>
            <Dropdown trigger="hover">
              <Button icon="ios-more" />
              <template #list>
                <DropdownMenu>
                  <DropdownItem>新对话</DropdownItem>
                  <DropdownItem>Markdown导出</DropdownItem>
                  <DropdownItem @click="toAgentDetail">应用详情</DropdownItem>
                </DropdownMenu>
              </template>
            </Dropdown>
          </div>
          <Chatbot />
        </div>
      </div>
    </div>
  </div>
</template>
<script setup>
import { ref, provide } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { navTabStore } from '@/store/navTab';

import { agentList } from '@/api/modules/agent';

import Chatbot from './components/Chatbot.vue';

const route = useRoute();
const router = useRouter();
const { delVisitedRoute } = navTabStore();

const back = () => {
  router.replace('/workspace');
  delVisitedRoute(route.path);
};

const activeAgentId = ref('');
const activeAgentName = ref('');
const dataList = ref([]);
// 获取列表
const getList = async (reset) => {
  if (reset) {
    pageIndex.value = 1;
  }
  try {
    const { data } = await agentList({
      query: {
        p: 1,
        ps: 10000,
      },
      data: {
        kbtype: undefined,
        aitype: 'agent',
      },
    });
    dataList.value = data.topicList;
    activeAgentId.value = data.topicList && data.topicList[0].id;
    activeAgentName.value = data.topicList && data.topicList[0].name;
  } finally {
  }
};
getList();

const handleAgentChange = (item) => {
  activeAgentId.value = item.id;
  activeAgentName.value = item.name;
};

const toAgentDetail = () => {
  router.replace('/app?id=' + activeAgentId.value);
  // delVisitedRoute(route.path);
};

provide('aiid', activeAgentId);
</script>

<style lang="scss" scoped>
.chat-wrapper {
  width: 100%;
  height: 100%;
  display: flex;
  background-color: #f4f4f5;
  border: 1px solid #e2e8f0;
  .left {
    width: 200px;
    border-right: 1px solid #e2e8f0;
  }
  .right {
    width: calc(100% - 200px);
    padding: 16px;
  }
  .chat {
    display: flex;
    width: 100%;
    height: 100%;
    background-color: #fff;
    border: 1px solid #dfe2ea;
    border-radius: 16px;
    overflow: hidden overlay;
  }
  .back {
    display: flex;
    align-items: center;
    padding: 16px;
    border-bottom: 1px solid #e2e8f0;
    cursor: pointer;
    &:hover {
      background-color: #e8ebf0;
    }
    &-icon {
      width: 36px;
      height: 36px;
      line-height: 36px;
      text-align: center;
      border-radius: 50%;
      background-color: #fff;
      cursor: pointer;
      box-shadow: rgba(0, 0, 0, 0.15) 1px 1px 9px;
    }
    &-text {
      font-size: 16px;
      margin-left: 12px;
    }
  }
  .agent {
    height: calc(100% - 90px);
    overflow: hidden;
    &-header {
      padding: 5px;
    }
    &-list {
      height: 100%;
      padding: 16px;
      overflow-y: auto;
    }
    &-item {
      padding: 8px 12px;
      margin-bottom: 12px;
      cursor: pointer;
      border-radius: 8px;
      -webkit-box-align: center;
      align-items: center;
      font-size: 14px;
      color: rgb(72, 82, 100);
      &:hover {
        background-color: #e8ebf0;
      }
      &.active {
        background-color: #fff;
        box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1),
          0 2px 4px -1px rgba(0, 0, 0, 0.06);
        color: #3370ff;
      }
    }
  }
}

.chat-info {
  width: 250px;
  padding: 16px;
  border-right: 1px solid #e8ebf0;
  .name {
    font-size: 16px;
    font-weight: bold;
    color: #333;
    padding: 10px;
  }
  .refresh {
    width: 100%;
    padding: 20px 0;
    display: flex;
    align-items: center;
    .add-btn {
      flex: 1;
      margin-right: 10px;
    }
  }
  .room-item {
    display: flex;
    align-items: center;
    height: 44px;
    cursor: pointer;
    user-select: none;
    border-radius: 8px;
    font-size: 14px;
    margin-bottom: 8px;
    padding: 0 12px;
    color: #485264;
    &.active {
      color: #3370ff;
      background-color: #f0f4ff;
    }
    &:hover {
      background-color: #f0f4ff;
    }
    .room-name {
      flex: 1;
      margin: 0 6px;
      text-overflow: ellipsis;
      overflow: hidden;
      white-space: nowrap;
    }
    .time {
      font-size: 12px;
      color: #485264;
    }
  }
  .more-text {
    font-size: 12px;
    color: #aaa;
    text-align: center;
    margin-top: 16px;
  }
}
.chat-content {
  flex: 1;
  position: relative;
  .header {
    display: flex;
    align-items: center;
    width: 100%;
    padding: 16px;
    border-bottom: 1px solid #e8ebf0;
    background-color: #fbfbfc;
    .chat-name {
      flex: 1;
    }
  }
  .content {
    width: 100%;
    height: calc(100% - 180px);
    overflow-y: auto;
    padding: 10px 20px;
    position: relative;
  }
}
</style>
